Skip to content

Limit CodeActions within passed range #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2021

Conversation

aufarg
Copy link
Contributor

@aufarg aufarg commented Feb 25, 2021

Return only actions within range specified by client.

Fixes: #1341

@aufarg aufarg force-pushed the narrow-codeactions-to-range branch from d33f980 to e6c866f Compare February 25, 2021 15:19
@berberman
Copy link
Collaborator

Does the fix work? Could you please add a test?

@aufarg
Copy link
Contributor Author

aufarg commented Feb 25, 2021

Does the fix work? Could you please add a test?

It does work, I'm writing the tests but I'm stuck because I can't write the following:

firstLine <- getCodeActions doc (mkRange 0 0 0 0)

let hasApplyAll = isJust . find (\ca -> "Apply all hints" `T.isSuffixOf` (ca ^. L.title))

liftIO $ hasApplyAll firstLine @? "There is no apply all code action"

Any pointer?

@aufarg
Copy link
Contributor Author

aufarg commented Feb 25, 2021

Nevermind, I'm dumb. I should map it with fromAction first

@michaelpj
Copy link
Collaborator

Should we audit the other instances of CodeActionParams to see if they should be using the range argument?

@aufarg aufarg force-pushed the narrow-codeactions-to-range branch 2 times, most recently from 6e5553e to cd1580c Compare February 25, 2021 17:13
@aufarg
Copy link
Contributor Author

aufarg commented Feb 25, 2021

@berberman
What are some available diagnostics that spans multiline in HLS? My changes will filter out some multiline diagnostics if code action range is strictly contained within the multiline. It can be potentially be fixed by doing subRange range caRange || subRange caRange range I suppose, what do you think?

Comment on lines 284 to 304
if numHintsInDoc > 1 then do
if numHintsInRange > 1 then do
pure $ applyAllAction:applyOneActions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should make sure that applyAllAction is not always prepended to applyOneActions, rather than changing numHintsInDoc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it okay since it's only used to determine if we should prepend to applyOneActions? And since I add more filtering, numHintsInRange <= numHintsInDoc is always true so the condition in the change already includes numHintsInDoc > 1 too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I just I realized that Apply all hints does not mean applying all hints for those range, but it means applying all the hints in doc. I've changed the condition

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should show applyAllAction iff:

  • there are more than one hlint diagnostics in the document
  • there is at least one hlint diagnostic in the current code action context

right? So based on numHintsInDoc, which has already implemented the logic for the first condition, you just need take a look at diagnostics in the current code action context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I tried just adding not (null diags) and it works. The problem now is that lsp-test sends all the diagnostics instead of only those within range.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong, but my understanding here is:

They are provided so that the server knows which errors are currently presented to the user for the given range

The client gives us the diagnostics that is shown to the user. If in the meantime the diagnostics changes, shouldn't the client issue the request again? We don't know for sure if the new diagnostics changes is already shown to the user (or even received by the client) and we might suggest actions that mismatch with what the user sees.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no guarantee that these accurately reflect the error state of the resource. The primary parameter to compute code actions is the provided range.

IMO this is the key bit.

Copy link
Contributor Author

@aufarg aufarg Mar 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but I'm not sure if this is possible:

  1. Client receives and shows diagnostic dA
  2. Client issues code action request with dA as context
  3. New diagnostic dB is sent from the server
  4. Server sends code action based on dB (since it's latest state)
  5. Client receives only code action suggestion but not the diagnostic message from (3) (maybe it's lost or something?)

In this case client will show diagnostic dA with code action dB.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, consider that AFAICT this is not mandatory, so if a client simply hasn't implemented adding the extra diagnostics, then this feature will not work, despite us having access to a perfectly good range we can use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they're optional? The definition doesn't have ? in the name.

@aufarg aufarg force-pushed the narrow-codeactions-to-range branch 2 times, most recently from 727607e to 3471a28 Compare February 26, 2021 04:34
@jneira jneira self-requested a review February 26, 2021 21:39
@aufarg aufarg force-pushed the narrow-codeactions-to-range branch from 3471a28 to ca92689 Compare February 27, 2021 17:20
, validCommand d
, Just nfp == docNfp
]
numHintsInContext = length
[d | d <- diags
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this also assuming that the diagnostics we in the context are exclusively hlint hints? I'm pretty sure that's not true: you could get other warnings or errors as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the impression that validCommand checks that, is it not the case?

Return only actions within range specified by client.
@aufarg aufarg force-pushed the narrow-codeactions-to-range branch from f0796d1 to cadeb78 Compare April 25, 2021 17:54
@aufarg
Copy link
Contributor Author

aufarg commented Apr 26, 2021

@berberman I've rebased the changes since we're already using lsp-test 0.14.0.0. The test passed now.

@aufarg aufarg requested review from berberman and michaelpj April 26, 2021 03:32
Copy link
Collaborator

@berberman berberman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@jneira jneira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@jneira jneira added the merge me Label to trigger pull request merge label Apr 26, 2021
@mergify mergify bot merged commit 4e10c8d into haskell:master Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge me Label to trigger pull request merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code action "Apply all hints" shows everywhere
4 participants